package com.iqiu.cms.util;


import com.gexin.rp.sdk.base.IBatch;
import com.gexin.rp.sdk.base.IIGtPush;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class AppPush {

    //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
    private static String appId = "JusFoZWKCZ5OVPiSkZFCKA";
    private static String appKey = "hMLls5sHRLA1D4kHkWCpe4";
    private static String masterSecret = "LiXh6SM58j7wqW74ndhkb2";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";

    /**
     * 通知多个
     * @param clientIds  通知人客户端id
     * @param title  通知栏标题
     * @param text  通知栏内容
     * @param param  参数 格式为JSON字符串 {'id':10001,'qita':'测试'}
     * @throws IOException
     */
    public static String pushMany(Set<String> clientIds, String title, String text, String param)  {

        IIGtPush push = new IGtPush(appKey, masterSecret,true);
        IBatch batch = push.getBatch();

        try {
            for (String cid:clientIds) {
                //构建透传消息
                constructClientTransMsg(cid,title,text,param,batch);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        IPushResult ret = null;
        try {
            ret = batch.submit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ret != null) {
            return ret.getResponse().toString();
        } else {
            return "服务器响应异常";
        }
    }
    /**
     * 通知单个用户
     */
    public static String  pushSingle(String clientid,String title,String text,String param) {
        if(StringUtils.isEmpty(clientid) ){
            return "";
        }

        IIGtPush push = new IGtPush(appKey, masterSecret,true);

        SingleMessage message = new SingleMessage();
        message.setData(template(title,text,param));
        message.setOffline(true);
        //离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 1000 * 3600);

        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(clientid);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            return ret.getResponse().toString();
        } else {
            return "服务器响应异常";
        }
    }
    /**
     * 群发所有人
     */
    public static String  pushAll(String title,String text,String param) {

        IIGtPush push = new IGtPush(appKey, masterSecret,true);

        AppMessage message = new AppMessage();
        message.setData(template(title,text,param));
        message.setOffline(true);
        //离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        List<String> appIdList = new ArrayList<String>();
        appIdList.add(appId);
        message.setAppIdList(appIdList);
        IPushResult ret = push.pushMessageToApp(message);
        if (ret != null) {
            return ret.getResponse().toString();
        } else {
            return "服务器响应异常";
        }
    }







    private static void constructClientTransMsg(String cid, String title,String text,String param ,IBatch batch) throws Exception {

        SingleMessage message = new SingleMessage();

        message.setData(template(title,text,param));
        message.setOffline(true);
        message.setOfflineExpireTime(24 * 1000 * 3600);

        // 设置推送目标，填入appid和clientId
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        batch.add(message, target);
    }

    public static NotificationTemplate template(String title,String text,String param){
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(title);
        style.setText(text);
        // 配置通知栏网络图标
        //style.setLogoUrl("http://img-cdn-qiniu.dcloud.net.cn/uploads/nav_menu/10.jpg");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(1);
        template.setTransmissionContent(param);
        return template;
    }



    public static void main(String[] args) throws Exception{
        System.out.println(AppPush.pushAll("赣州团队测试","text赣州团队测试","{'id':1000}"));
    }
}